package com.ohos.carbon.slice.demos;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DemoDataBean;
import com.ohos.carbon.provider.DemosAdapter;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.ResourceTable.Graphic_background_button_gray;
import static com.ohos.carbon.ResourceTable.Graphic_background_color_white;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class MusicplayerSlice extends AbilitySlice {

    private Image back_button, preButton, playButton, nextButton;
    private Text text_title;
    private ListContainer listContainer;
    private ArrayList<DemoDataBean> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_musicplayer);
        initData();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Music player")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Music player")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Music player", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Music player", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Music player", 1);
            pf.flushSync();
        }
    }

    private void initData() {
        mList = new ArrayList<>();
        mList.add(new DemoDataBean(0, "1", "Code Around the Clock", null, "3:33"));
        mList.add(new DemoDataBean(0, "2", "Compile Yourself", null, "3:45"));
        mList.add(new DemoDataBean(0, "3", "OHOSic Dreaming", null, "2:47"));
        mList.add(new DemoDataBean(0, "4", "Carbonite Tonght", null, "3:12"));
        mList.add(new DemoDataBean(0, "5", "Getting Away With Errors", null, "3:34"));
        mList.add(new DemoDataBean(0, "6", "Nine Million XMLs", null, "3:22"));
        mList.add(new DemoDataBean(0, "7", "Last Commit in the Repo", null, "4:36"));
        mList.add(new DemoDataBean(0, "8", "Don't Fix Me Down", null, "3:54"));

    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Music player");
        /*image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image1.setVisibility(Component.HIDE);
        image2.setVisibility(Component.HIDE);*/

        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        listContainer.setItemProvider(new DemosAdapter(mList, getContext()));
        back_button.setClickedListener(component -> onBackPressed());

        preButton = (Image) findComponentById(ResourceTable.Id_pre_button);
        playButton = (Image) findComponentById(ResourceTable.Id_play_button);
        nextButton = (Image) findComponentById(ResourceTable.Id_next_button);

        preButton.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        preButton.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_button_gray));
                        break;
                    case TouchEvent.CANCEL:
                    case TouchEvent.PRIMARY_POINT_UP:
                        preButton.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_white));
                        break;
                }
                return true;
            }
        });

        playButton.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        playButton.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_button_gray));
                        break;
                    case TouchEvent.CANCEL:
                    case TouchEvent.PRIMARY_POINT_UP:
                        playButton.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_white));
                        break;
                }
                return true;
            }
        });

        nextButton.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        nextButton.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_button_gray));
                        break;
                    case TouchEvent.CANCEL:
                    case TouchEvent.PRIMARY_POINT_UP:
                        nextButton.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_white));
                        break;
                }
                return true;
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
